﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;

namespace HVACWebService.Common
{
    /// <summary>
    /// HttpWebRequestHelp 的摘要说明
    /// </summary>
    public class HttpWebRequestHelp
    {

        public T PostData<T>(string url, string body) where T : class
        {
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.Method = "POST";
            httpWebRequest.Timeout = 20000;

            if (body != "")
            {
                byte[] btBodys = Encoding.UTF8.GetBytes(body);
                httpWebRequest.ContentLength = btBodys.Length;
                httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
            }

            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            try
            {
                using (Stream resStream = httpWebResponse.GetResponseStream())
                {
                    var reader = new StreamReader(resStream, Encoding.Default);
                    var respText = reader.ReadToEnd();
                    if (resStream != null) resStream.Close();
                    return JsonSerializer.Instance.Deserialize<T>(respText);
                }
            }
            catch (Exception)
            {

                return null;
            }
        }


        public string PostData(string url, string body)
        {
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.Method = "POST";
            httpWebRequest.Timeout = 20000;

            if (body != "")
            {
                byte[] btBodys = Encoding.UTF8.GetBytes(body);
                httpWebRequest.ContentLength = btBodys.Length;
                httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
            }

            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
            string responseContent = streamReader.ReadToEnd();
            httpWebResponse.Close();
            streamReader.Close();
            httpWebRequest.Abort();
            httpWebResponse.Close();
            return responseContent;
        }

        /// <summary>
        /// 无返回数据，只发送请求
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public void GetNoReturnDate(string url)
        {
            var request = (HttpWebRequest)WebRequest.Create(url);
            request.GetResponse();
        }

        /// <summary>
        /// 获取数据 返回 T 类型数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <returns></returns>
        public T GetData<T>(string url) where T : class
        {
            var request = (HttpWebRequest)WebRequest.Create(url);
            var response = (HttpWebResponse)request.GetResponse();

            try
            {
                using (Stream resStream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(resStream, Encoding.Default);
                    var respText = reader.ReadToEnd();
                    if (resStream != null) resStream.Close();
                    return JsonSerializer.Instance.Deserialize<T>(respText);
                }
            }
            catch (Exception)
            {

                return null;
            }
        }

        /// <summary>
        /// 获取数据，返回 key 的Value
        /// </summary>
        /// <param name="url"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetDataByKey(string url, string key)
        {
            string respText = "";
            string keyValue = "";
            var request = (HttpWebRequest)WebRequest.Create(url);
            var response = (HttpWebResponse)request.GetResponse();
            using (var resStream = response.GetResponseStream())
            {
                var reader = new StreamReader(resStream, Encoding.Default);
                respText = reader.ReadToEnd();
                if (resStream != null) resStream.Close();
            }
            var Jss = new JavaScriptSerializer();
            Dictionary<string, object> respDic = (Dictionary<string, object>)Jss.DeserializeObject(respText);
            //通过键access_token获取值
            keyValue = respDic[key].ToString();

            return keyValue;
        }

        /// <summary>
        /// 获取数据，返回 key 的Value
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string GetDataString(string url)
        {
            var respText = "";
            var request = (HttpWebRequest)WebRequest.Create(url);
            var response = (HttpWebResponse)request.GetResponse();
            using (var resStream = response.GetResponseStream())
            {
                var reader = new StreamReader(resStream, Encoding.Default);
                respText = reader.ReadToEnd();
                if (resStream != null) resStream.Close();
            }

            return respText;
        }



        /// <summary>
        /// 获取数据，返回 key 的Value
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string GetDataStringUTF_8(string url)
        {
            var respText = "";
            var request = (HttpWebRequest)WebRequest.Create(url);
            var response = (HttpWebResponse)request.GetResponse();
            using (var resStream = response.GetResponseStream())
            {
                var reader = new StreamReader(resStream, Encoding.UTF8);
                respText = reader.ReadToEnd();
                if (resStream != null) resStream.Close();
            }

            return respText;
        }



        public string PostDataStringUTF_8(string url, string body)
        {
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.Method = "POST";
            httpWebRequest.Timeout = 20000;

            if (body != "")
            {
                byte[] btBodys = Encoding.UTF8.GetBytes(body);
                httpWebRequest.ContentLength = btBodys.Length;
                httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
            }

            var response = (HttpWebResponse)httpWebRequest.GetResponse();
            var respText = "";
            using (var resStream = response.GetResponseStream())
            {
                var reader = new StreamReader(resStream, Encoding.UTF8);
                respText = reader.ReadToEnd();
                if (resStream != null) resStream.Close();
            }

            return respText;
        }
    }
}